package dp;

import java.util.Arrays;

public class DpBert {

    public static void main(String[] args) {
        // [[0,0,0],[0,1,0],[0,0,0]]
        // int m = Integer.parseInt(args[0]);
        // int n = Integer.parseInt(args[1]);
        DpBert dpBert = new DpBert();
        // int sum = dpBert.uniquePaths(m, n);
        // int[][] obstacleGrid = {0,0,0,0,1,0,0,0,0};
        
        String str = "[[2,7,0,1],[0,1,4,9],[0,2,3,0]]";
        // 1、去除最外层[]
        str = str.substring(1,str.length()-1);
        String firstRow = str.substring(1,str.indexOf("]"));
        // System.out.println(firstRow);
        String[] firstArray = firstRow.split(",");
        int n = firstArray.length; // 列数
        System.out.println("col:"+n);
        String noBrack = str.replaceAll("\\[", "").replaceAll("]", "");
        System.out.println(noBrack);
        String[] strArray = noBrack.split(",");
        int m = strArray.length / n ; // 行数
        System.out.println("row:" + m);
        int[][] arr = new int[m][n];
        int index = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                arr[i][j] = Integer.parseInt(strArray[index]);
                index++;
            }
        }
        System.out.println(Arrays.asList(arr));
    }
    // 输入字符串转二维数组
    // [[0,0,0],[0,1,0],[0,0,0]]
    public int[][] strToArray(String str) {
        // 1、去除最外层[]
        str = str.substring(1,str.length()-1);
        str.split(str)
        return null;
    }
    // leetcode63 有障碍的不同路径
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for(int i = 0; i < m && obstacleGrid[i][0]==0; i++) {
            dp[i][0] = 1;
        }
        for(int j = 0; j < n && obstacleGrid[0][j]==0; j++) {
            dp[0][j] = 1;
        }
        
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                if(obstacleGrid[i][j] == 1) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = dp[i-1][j]+dp[i][j-1];
                }
            }
        }
        return dp[m-1][n-1];
    }
    
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];

        for(int i=1; i < m; i++){
            for(int j=1; j < n; j++) {
                if(dp[i-1][j] == 0) {
                    dp[i-1][j] = 1;
                }
                if(dp[i][j-1] == 0) {
                    dp[i][j-1] = 1;
                }
                dp[i][j] = dp[i-1][j]+dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}
